﻿using Durian.API.Application.Commands.Assembly;
using IMES.Common.Domain.DurianModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics.Contracts;
using System.Linq;

namespace Durian.API.Controllers
{
    /// <summary>
    /// 装配工单控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AssemblyController : ControllerBase
    {
        private readonly IMediator mediator;

        public AssemblyController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 装配工单添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<Result> Handle(AssemblyCreateCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 装配工单显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<Result> Handle1(AssemblySelectCommand request)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        /// 反填装配工单信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<ResultT<Assemblies>> Handle2(AssemblyUntilCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 修改装配工单信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public async Task<Result> Handle3(AssemblyUpdateCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 批量删除装配工单信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete]
        public async Task<Result> Handle4(AssemblyDeleteCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 导出装配工单
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportFarrowingsExcel([FromQuery] ExcelFarrowingsCommand cmd)
        {
            var res = await mediator.Send(cmd);
            return File(res.FileBytes, res.MimeType, res.FileName);
        }

        /// <summary>
        /// 导入装配工单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> Handle5(FileExportCommand cmd)
        {
            var result = await mediator.Send(cmd);
            return result;
        }

        /// <summary>
        /// 打印装配工单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> Handle6(OrderAssemblySearchCommand cmd)
        {
            return await mediator.Send(cmd);
        }
    }
}
